﻿Imports System.Data
Imports PadovadoAPI
Imports System.Web.Routing

Partial Class TabWeek
    Inherits System.Web.UI.UserControl

    Protected Property currentDate As DateTime
        Set(value As DateTime)
            ViewState("currentDate") = value
        End Set
        Get
            If IsNothing(ViewState("currentDate")) Then
                ViewState("currentDate") = DateTime.Now.Date()
            End If
            Return ViewState("currentDate")
        End Get
    End Property


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not (IsPostBack) Then

            Calendar1.SelectedDate = currentDate

        End If
        CaricaRepeater()
        AggiornaBottoni()
    End Sub

    Protected Sub Calendar1_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Calendar1.SelectionChanged
        currentDate = Calendar1.SelectedDate.Date()
        '  currentDate = Calendar1.VisibleDate
        CaricaRepeater()
        AggiornaBottoni()


    End Sub


    Public Sub AggiornaBottoni()
        LblData1.Text = currentDate.ToString("dddd dd")
        LblData2.Text = currentDate.AddDays(1).ToString("dddd dd")
        LblData3.Text = currentDate.AddDays(2).ToString("dddd dd")
        LblData4.Text = currentDate.AddDays(3).ToString("dddd dd")
        LblData5.Text = currentDate.AddDays(4).ToString("dddd dd")
        LblData6.Text = currentDate.AddDays(5).ToString("dddd dd")
        LblData7.Text = currentDate.AddDays(6).ToString("dddd dd")
    End Sub

    Public Sub CaricaRepeater()
        'Dim diz As New Dizionario
        Dim sql As String = ""
        ' Try

        Dim ds As New DataSet
        Dim FK_target As New Integer
        Dim dbConnector As New DBObject

        If (Not String.IsNullOrEmpty(Request.QueryString("FK_Target"))) Then
            FK_target = Request.QueryString("FK_Target")
        End If
        Dim rptEventis As New List(Of Repeater)
        rptEventis.Add(RptEventi1)
        rptEventis.Add(RptEventi2)
        rptEventis.Add(RptEventi3)
        rptEventis.Add(RptEventi4)
        rptEventis.Add(RptEventi5)
        rptEventis.Add(RptEventi6)
        rptEventis.Add(RptEventi7)
        For i As Integer = 0 To 6
            sql = "SELECT E.NomeEvento, I.Comune, E.Descrizione as DescrizioneEvento, E.PK_evento," & _
                    "CASE isnull(E.imgEvento, '') WHEN '' then MAX(T.TargetImgURLSmall) else E.imgEvento end as ImgEvento," & _
                    " convert(varchar,E.DataInizio,103) as DataInizio, convert(varchar,E.DataFine,103) as DataFine, E.Link,  E.Brochure, " & _
                    " CASE isnull(E.link, '') WHEN '' then 0 else 1 end as LinkPresente, " & _
                    " CASE isnull(E.Brochure, '') WHEN '' then 0 else 1 end as BrochurePresente, " & _
                    " isnull(I.Strada,'') + ' ' + isnull(I.Civico,'') + ' ' + isnull(I.Comune,'') + ' ' + isnull(I.Presso,'') as Indirizzo, " & _
                   " MAX(T.TargetImgURLSmall), " & _
                   " A.Denominazione as Organizzatore, " & _
                     " CASE isnull(E.intCategoria, 100) " & _
                      "     WHEN 100 then 'Generico' " & _
                      "     WHEN 1 then 'Concerto'" & _
                      "     WHEN 2 then 'Festa'" & _
                      "     WHEN 3 then 'Discoteca'" & _
                      "     WHEN 4 then 'Fiera'" & _
                      "     WHEN 5 then 'Sagra'" & _
                      "     WHEN 6 then 'Gita'" & _
                      "     WHEN 7 then 'Gratis'" & _
                      "     WHEN 8 then 'Mostra' end as Genere, " & _
                   " ROW_NUMBER() OVER(ORDER BY E.DataInizio) AS 'RowNumber'" & _
                   " FROM Evento E " & _
                   " LEFT OUTER JOIN Indirizzo I ON I.PK_Indirizzo = FK_Indirizzo " & _
                   " LEFT OUTER JOIN Evento_Organizzatore EO ON PK_Evento = EO.FK_Evento " & _
                   " LEFT OUTER JOIN Organizzatore O ON O.PK_Organizzatore = EO.FK_Organizzatore " & _
                   " LEFT OUTER JOIN Anagrafica A ON A.PK_Anagrafica = O.FK_Anagrafica " & _
                   " LEFT OUTER JOIN Evento_Target ET ON ET.FK_Evento = PK_Evento " & _
                   " LEFT OUTER JOIN Target T ON T.PK_Target = ET.FK_Target " & _
                    " WHERE E.Deleted= 0 AND (E.DataInizio = CAST('" & currentDate.AddDays(i).ToString("yyyyMMdd") & "' AS DATE)" & _
                    " OR (E.DataInizio < CAST('" & currentDate.AddDays(i).ToString("yyyyMMdd") & "' as date) " & _
                    " AND E.DataFine >= CAST('" & currentDate.AddDays(i).ToString("yyyyMMdd") & "' as date)))"
            ' DBObject.WriteLog(sql)
            ' Throw New Exception(sql)
            If (FK_target = 6) Then
                sql &= " AND (E.IngressoLibero= 1)"
            ElseIf (FK_target <> 5) Then
                sql &= " AND (ET.FK_target = " & FK_target & " OR ET.FK_target = 5)"
            End If
            sql &= " GROUP BY E.NomeEvento, E.Descrizione, E.DataInizio, E.DataFine, E.Link, I.Strada, I.Civico, I.Comune, I.Presso, PK_Evento, E.imgEvento, E.Brochure, E.Priorita," & _
                " E.intCategoria, A.Denominazione" & _
                " ORDER BY E.Priorita Desc"
            'Throw New Exception(sql)
            ds = dbConnector.FillDS(sql, Nothing, 1)

            '<%#DataBinder.Eval(Container.DataItem, "ImageTargetId")%>'
            If Not IsNothing(ds) AndAlso ds.Tables.Count > 0 Then
                ds.Tables(0).Columns.Add("ItemTemplateId")
                ds.Tables(0).Columns.Add("ImageApriId")
                ds.Tables(0).Columns.Add("FK_Target")
                For Each dr As DataRow In ds.Tables(0).Rows
                    dr("ItemTemplateId") = "ItemTemplate_" & i & "_" & dr("RowNumber")
                    dr("ImageApriId") = "ImageApri_" & i & "_" & dr("RowNumber")
                    Dim parameters As RouteValueDictionary
                    parameters = New RouteValueDictionary(New With {.TipoLocale = Trim(dr("Genere")), .NomeLocale = Trim(dr("Organizzatore")), .TitoloEvento = Trim(dr("NomeEvento"))})

                    Dim vpd As VirtualPathData
                    vpd = RouteTable.Routes.GetVirtualPath(Nothing, "PaginaEvento", parameters)

                    dr("NomeEvento") = dr("Comune") & " - " & dr("DataInizio") & " - " & dr("Organizzatore") & " - " & dr("NomeEvento")
                    'dr("LinkInterno") = dr("LinkInterno") & "&NomeEvento=" & dr("NomeEvento")
                    dr("FK_Target") = vpd.VirtualPath & "?PK_Evento=" & dr("PK_Evento")
                Next


                ' Throw New Exception(ds.Tables.Count)
                rptEventis(i).DataSource = ds
                rptEventis(i).DataBind()
            End If
        Next

        ' Catch ex As Exception
        '  Throw New Exception(sql)
        '  End Try
    End Sub

End Class
